package com.ruoyi.applet.controller;

import java.util.List;
import java.io.IOException;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.applet.domain.ProductSpecs;
import com.ruoyi.applet.domain.vo.CartVo;
import com.ruoyi.applet.utli.TokenByUsrId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.applet.domain.Cart;
import com.ruoyi.applet.service.ICartService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 购物车Controller
 * 
 * @author ruoyi
 * @date 2024-12-04
 */
@RestController
@RequestMapping("/applet/cart")
public class CartController extends BaseController
{
    @Resource
    private ICartService cartService;

    /**
     * 查询购物车列表
     */
    @RequestMapping("/list")
    public Object list(@RequestParam("token") String token)
    {
        return cartService.selectCartList(TokenByUsrId.tokenByUsrId(token));
    }



    /**
     * 获取购物车详细信息
     */
    @RequestMapping("/selectByCart")
    public Object getInfo(@RequestParam("cartId") Long cartId)
    {
        return success(cartService.selectCartByCartId(cartId));
    }

    /**
     * 新增购物车
     */
    @RequestMapping("/insertCart")
    public Object add(@RequestParam("goodsId") Long goodsId,@RequestParam("token") String token ,
                      @RequestParam("buyNum") Long buyNum,@RequestParam("proSpescId") Long proSpescId)
    {
        Cart cart = cartService.selectByUserIdCart(proSpescId, TokenByUsrId.tokenByUsrId(token));
        ProductSpecs productSpecs = cartService.selectAllByProductSpecId(proSpescId);
        if(productSpecs.getProductStock() != 0) {
            if (cartService.selectAllByProductSpecIdInteger(proSpescId) <= 0) {
                return cartService.insertCart(goodsId, TokenByUsrId.tokenByUsrId(token), buyNum, proSpescId);
            }
            return cartService.updateProSpescIdBoolean(proSpescId,cart.getBuyNum() + buyNum,TokenByUsrId.tokenByUsrId(token));
        }
        return false;
    }

    @RequestMapping("/updateCart")
    public Object updateCart(@RequestParam("cartId") Long cartId,@RequestParam("isChecked") String isChecked){
        return cartService.updateCartIdBoolean(cartId, isChecked);
    }

    @RequestMapping("/delCart")
    public Object delCart(@RequestParam("cartId") Long cartId){
        return cartService.deleteByCartIdBoolean(cartId);
    }

    @RequestMapping("/updateCartByBuyNum")
    public Object updateCartByUserId(@RequestParam("token") String token,@RequestParam("cartId") Long cartId,
                                     @RequestParam("isChecked") String isChecked,@RequestParam("buyNum") Long buyNum){
        return cartService.updateIsCheckedAndBuyNumInteger(TokenByUsrId.tokenByUsrId(token),cartId,isChecked,buyNum);
    }
}